package com.tanhua.server.controller;

import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 登录注册控制层
 */
@RestController
@RequestMapping("/user")
public class LoginController {

    @Autowired
    private UserService userService;

    /**
     * 登录---获取验证码
     * ResponseEntity：响应实体对象 由spring提供的 返回码 返回信息 状态码等
     *
     * Body:请求体 @RequestBody接收
     */
    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public ResponseEntity sendCode(@RequestBody Map<String,String> params){
        String phone = params.get("phone");//用户输入的手机号
        userService.sendCode(phone);
        //ResponseEntity.ok(不需要返回数据的时候 设置为null); ok：HttpStatus.OK == 200状态码
        return ResponseEntity.ok(null);
    }

    /**
     * 登录---验证码校验
     */
    @RequestMapping(value = "/loginVerification",method = RequestMethod.POST)
    public ResponseEntity login(@RequestBody Map<String,String> params){
        String phone = params.get("phone");//用户输入的手机号
        String verificationCode = params.get("verificationCode");//用户输入的验证码
        Map map = userService.login(phone,verificationCode);//map中 {“token”:“字符串”,isNew:true}
        return ResponseEntity.ok(map);
    }
}
